<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="-mb-15px" label-width="68px">
      <el-form-item label="订单编号" prop="orderNum">
        <el-input v-model="queryParams.orderNum" class="!w-300px" clearable placeholder="请输入订单编号"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" class="!w-300px" placeholder="请选择订单状态" clearable>
          <el-option v-for="item in getIntDictOptions(DICT_TYPE.ORDER_STATUS)" :key="item.value" :label="item.label"
            :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-280px" end-placeholder="结束日期"
          start-placeholder="开始日期" type="daterange" value-format="YYYY-MM-DD HH:mm:ss" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="订单编号" align="center" prop="orderNum" :show-overflow-tooltip="true" />
      <el-table-column label="订单内容" align="center" prop="orderContent">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ORDER_CONTENT" :value="scope.row.orderContent" />
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ORDER_STATUS" :value="scope.row.orderStatus" />
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="center" prop="quote">
        <template #default="scope">
          <el-tooltip v-if="scope.row.quoteFile" class="box-item" effect="dark" content="点击下载报价单" placement="top">
            <el-link :href="scope.row.url" target="_blank">
              {{ scope.row.quote === null ? '-' : scope.row.quote + '元' }}
            </el-link>
          </el-tooltip>
          <span v-else>{{ scope.row.quote === null ? '-' : scope.row.quote + '元' }} </span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <div class="flex items-center justify-center">
            <el-button type="primary" link @click="handleInfo(scope.row.orderId)">
              订单详情
            </el-button>
            <el-button type="primary" link
              v-if="scope.row.orderStatus !== 0 && scope.row.payStatus !== 2 && scope.row.orderStatus != 6"
              @click="handlePayOrder(scope.row)">
              支付订单
            </el-button>
            <el-button type="danger" link @click="handleDelete(scope.row.orderId)" v-if="scope.row.orderStatus != 6">
              关闭订单
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNo" :total="total"
      @pagination="getList" />
  </ContentWrap>
  <!-- 支付订单 -->
  <PayOrder ref="payOrderRef" @success="getList" />
  <!-- 关闭订单 -->
  <CloseReason ref="closeReasonRef" @success="getList" />
</template>
<script setup>
import * as OrderApi from '@/api/order'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import PayOrder from './payOrder.vue'
import { useRouter } from 'vue-router'
import CloseReason from '../closeReason.vue'
const router = useRouter()
const message = useMessage() // 消息弹窗
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderStatus: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrderApi.getOrderList(queryParams)
    data?.records &&
      data.records.forEach((item) => {
        if (item.quoteFile) {
          const File = JSON.parse(item.quoteFile)
          item.url = File[0].url
        } else {
          item.url = ''
        }
      })
    list.value = data?.records || []
    total.value = data?.total || 0
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 支付订单 */
const payOrderRef = ref()
const handlePayOrder = (row) => {
  payOrderRef.value.open(row)
}

const closeReasonRef = ref()
/** 删除按钮操作 */
const handleDelete = async (orderId) => {
  closeReasonRef.value.open(orderId)
}

const handleInfo = (id) => {
  router.push({ path: '/orderDetail/orderDetailPage', query: { id } })
}

/** 初始化 */
onMounted(() => {
  getList()
})
</script>
<style scoped lang="scss"></style>
